package com.tongji.seller.service
{

	import com.tongji.seller.events.GetStoreFavoriteStateEvent;
	import com.tongji.seller.events.GetTopHotSceneEvent;
	import com.tongji.seller.events.GetStoreRecentOperateSceneStateEvent;

	import com.tongji.seller.events.GetStoreSavedSceneEvent;
	import com.tongji.seller.events.GetStoreSceneDetailEvent;

	import com.tongji.seller.events.GetStoreSystemSceneHotStateEvent;
	import com.tongji.seller.events.GetStoreSystemSceneStateEvent;
	import com.tongji.seller.events.GetStoreUserSceneEvent;
	import com.tongji.seller.model.UserModel;
	
	import flash.system.Security;
	
	import mx.controls.Alert;
	import mx.messaging.channels.StreamingAMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.WebService;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author chenzhongyuan
	 * 2011-12-6
	 */
	
	public class StoreSceneService extends Actor
	{
		private var ws:WebService;
		[Inject]
		public var userModel:UserModel;
		public function StoreSceneService()
		{
			super();
			ws = new WebService();
			ws.wsdl = "http://share.rryjw.com/VRService.asmx?wsdl";
			ws.useProxy = false;
			ws.loadWSDL();
			Security.loadPolicyFile("http://img.rryjw.com/crossdomain.xml");
		}
		
		/**
		 * 获取店铺前12的热门场景
		 * @param topNum
		 * 
		 */		
		
		public function getTopHotScene(topNum:int):void
		{
			ws.GetStoreHotScene(topNum);
			ws.GetStoreHotScene.addEventListener(ResultEvent.RESULT, getHotSceneResult);
			ws.GetStoreHotScene.addEventListener(FaultEvent.FAULT, faultHandler);
		}	
		
		/**
		 * 获取用户保存的系统场景
		 * @param userId
		 * 
		 */				
		public function getStoreUserSystemScene(userId:Number):void
		{
			ws.GetStoreUserSystemScene(userId);
			ws.GetStoreUserSystemScene.addEventListener(ResultEvent.RESULT, getStoreUserSystemSceneResult);
			ws.GetStoreUserSystemScene.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		

		
		/**
		 * 获取店铺用户收藏的的场景信息带保存状态
		 * @param userID
		 * @param currentPage
		 * @param pageSize
		 * 
		 */				
		public function getStoreFavoriteState(userID:int, currentPage:int, pageSize:int):void
		{
			ws.GetStoreFavoriteState(userID, currentPage, pageSize);
			ws.GetStoreFavoriteState.addEventListener(ResultEvent.RESULT, getStoreFavoriteStateHandler);
			ws.GetStoreFavoriteState.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		/**
		 * 获取店铺版的系统场景信息带保存和收藏状态
		 * @param userID
		 * @param scjId
		 * @param sfgId
		 * @param currentPage
		 * @param pageSize
		 * 
		 */		
		public function getStoreSystemSceneState(userID:int, scjId:String, sfgId:String, currentPage:int, pageSize:int):void
		{
			ws.GetStoreSystemSceneState(userID, scjId, sfgId, currentPage, pageSize);
			ws.GetStoreSystemSceneState.addEventListener(ResultEvent.RESULT, getStoreSystemSceneStateHandler);
			ws.GetStoreSystemSceneState.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		/**
		 * 获取店铺用户最近使用的的场景信息带保存和收藏状态
		 * @param userID
		 * @param currentPage
		 * @param pageSize
		 * 
		 */		
		public function getStoreRecentOperateSceneState(userID:int, currentPage:int, pageSize:int):void
		{
			ws.GetStoreRecentOperateSceneState(userID, currentPage, pageSize);
			ws.GetStoreRecentOperateSceneState.addEventListener(ResultEvent.RESULT, getStoreRecentOperateSceneStateHandler);
			ws.GetStoreRecentOperateSceneState.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		/**
		 *  获取店铺版的热门系统场景信息带保存和收藏状态
		 * @param userID
		 * @param scjId
		 * @param sfgId
		 * @param currentPage
		 * @param pageSize
		 * 
		 */		
		public function getStoreSystemSceneHotState(userID:int, scjId:String, sfgId:String, currentPage:int, pageSize:int):void
		{
			ws.GetStoreSystemSceneHotState(userID, scjId, sfgId, currentPage, pageSize);
			ws.GetStoreSystemSceneHotState.addEventListener(ResultEvent.RESULT, getStoreSystemSceneHotStateHandler);
			ws.GetStoreSystemSceneHotState.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		/**
		 * 根据场景ID获取该场景的详细信息 
		 * @param sid
		 * 
		 */		
		public function getSceneDetail(sid:Number):void
		{
			/*ws.GetStoreSceneBySid(sid);
			ws.GetStoreSceneBySid.addEventListener(ResultEvent.RESULT, getSceneDetailResult);
			ws.GetStoreSceneBySid.addEventListener(FaultEvent.FAULT, faultHandler);*/
			ws.GetStoreUserSceneBgImgBySid(198);
			ws.GetStoreUserSceneBgImgBySid.addEventListener(ResultEvent.RESULT, getSceneDetailResult);
			ws.GetStoreUserSceneBgImgBySid.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		private function getHotSceneResult(event:ResultEvent):void
		{
			dispatch( new GetTopHotSceneEvent(GetTopHotSceneEvent.UPDATE_HOT_SCENE, 0, event.result as String));
		}
		
		private function getStoreUserSystemSceneResult(event:ResultEvent):void
		{
			dispatch( new GetStoreSavedSceneEvent(GetStoreSavedSceneEvent.UPDATE_SAVED_SCENE, 0, event.result as String));
		}
		
		/**
		 * 获取店铺用户收藏的的场景信息
		 * @param userID
		 * @param currentPage
		 * @param pageSize
		 * 
		 */		
		
	/*	public function getStoreFavoriteScene(userID:int, currentPage:int, pageSize:int):void
		{
			ws.GetStoreFavoriteScene(userID, currentPage, pageSize);
			ws.GetStoreFavoriteScene.addEventListener(ResultEvent.RESULT, getStoreFavoriteSceneResult);
			ws.GetStoreFavoriteScene.addEventListener(FaultEvent.FAULT, faultHandler);
		}*/
		
	/*	private function getStoreFavoriteSceneResult(event:ResultEvent):void
		{
			dispatch(new GetStoreFavoriteSceneEvent(GetStoreFavoriteSceneEvent.UPDATE_STORE_FAVORITE_SCENE, 1, 1, 1, event.result as String));
		}*/
		
		/**
		 * 最近使用的场景 
		 * @param userID
		 * @param currentPage
		 * @param pageSize
		 * 
		 */		
		/*public function getStoreSceneInformation(userID:int, currentPage:int, pageSize:int):void
		{
			ws.GetStoreRecentOperateScene(userID, currentPage, pageSize);
			ws.GetStoreRecentOperateScene.addEventListener(ResultEvent.RESULT, getSceneInformHandler);
			ws.GetStoreRecentOperateScene.addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		private function getSceneInformHandler(event:ResultEvent):void
		{
			dispatch(new GetStoreRecentSceneEvent(GetStoreRecentSceneEvent.UPDATE_SCENE, 1, 1, 1, event.result as String));
		}*/
		

		
		private function getStoreFavoriteStateHandler(event:ResultEvent):void
		{
			dispatch(new GetStoreFavoriteStateEvent(GetStoreFavoriteStateEvent.UPDATE_STORE_FAVORITE_STATE, userModel.currentUserID, 1, 18, event.result as String));
		}

		private function getStoreSystemSceneStateHandler(event:ResultEvent):void
		{
			dispatch(new GetStoreSystemSceneStateEvent(GetStoreSystemSceneStateEvent.UPDATE_STORE_SYSTEM_SCENE_STATE, userModel.currentUserID, null, null, 1, 18, event.result as String));
		}
		
		private function getStoreRecentOperateSceneStateHandler(event:ResultEvent):void
		{
			dispatch(new GetStoreRecentOperateSceneStateEvent(GetStoreRecentOperateSceneStateEvent.UPDATE_STORE_RECENT_OPERATE_SCENE_STATE, userModel.currentUserID, 1, 18, event.result as String));
		}

		private function getStoreSystemSceneHotStateHandler(event:ResultEvent):void
		{
			dispatch(new GetStoreSystemSceneHotStateEvent(GetStoreSystemSceneHotStateEvent.UPDATE_STORE_SYSTEM_SCENE_HOT_STATE, userModel.currentUserID, null, null, 1, 18, event.result as String));
		}
		
		private function getSceneDetailResult(event:ResultEvent):void
		{
			dispatch(new GetStoreSceneDetailEvent(GetStoreSceneDetailEvent.UPDATE_SCENE_DETAIL,0, event.result as String)); 
		}

		private function faultHandler(event:FaultEvent):void
		{
			Alert.show("失败" + event.fault.toString());
		}	
		
		
	}
}